class UsersController < ApplicationController
  def create
	redirect_to user_path current_user_name
  end

  def show
	if !current_user?
		@user = User.new
		@user.name = current_user_name
		if !@user.save
			flash[:error] = @user.errors.full_messages
			render 'welcomes/index'
		end
	else
	end
  end
  
  def proxy
	if current_user?
		current_user.proxy = params[:user][:proxy]
		current_user.save
		redirect_to user_path current_user_name
	end
  end
  
  def unblock
	if current_user?
		current_user.new_unblock params[:user][:url]
		redirect_to user_path current_user_name
	end
  end

  def pac
	if current_user.proxy && current_user.proxy.size>0 
		@proxy = %Q{#{current_user.proxy}}
	end
	@hosts = @user.unblock.map do |e|
  		%Q{'#{e.host}'}
  	end .join(',')
	
	data = File.read "#{Rails.root}/public/template.pac"
	pac = ERB.new(data).result binding
	send_data pac, type: 'application/x-ns-proxy-autoconfig', filename: "#{params[:id]}.pac"
  end
  
  def current_user_name
	params[:id] || params[:user][:id]
  end
  
  def current_user
	@user ||= User.find_by_name current_user_name
  end
  
  def current_user?
	!!current_user
  end
end
